<!-- 生产报表 - 生产进度查询 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :search.sync="search"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true" :loading="exportBtnLoading">导 出</el-button>
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-button type="text" size="small" icon="el-icon-view" @click="viewDetail(row)">详情</el-button>
      </template>
      <!-- <template slot="infoForm">
        <avue-crud ref="infoCrud" :option="infoOption" :data="infoData" @row-del="rowDel" v-model="infoForm" @selection-change="infoSelectionChange"> </avue-crud>
      </template> -->
      <!-- <template #workOrderNo="{ row }">
        <span style="color: #66b1ff; cursor: pointer" @click="goPath(row.workOrderNo)">{{ row.workOrderNo }}</span>
      </template> -->
    </avue-crud>
    <el-dialog class="showDialog" :title="'详情'" append-to-body :visible.sync="showDialog" :close-on-click-modal="false" top="5vh" width="80%" @close="closeDialog">
      <div class="sail-header-dialog-content">
        <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>基本信息</h3>
        <avue-form ref="basicInfoForm" v-if="showDialog" v-model="basicInfo" :option="basicInfoOption"></avue-form>
        <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>生产进展详情</h3>
        <avue-crud ref="productProgressForm" :option="productProgressOption" :data="productProgressData" @row-click="rowClick" :row-class-name="tableRowClassName"></avue-crud>
        <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>在制品信息</h3>
        <avue-crud ref="infoCrud" :option="infoOption" :data="infoData"></avue-crud>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <global-modal-template :visibile.sync="visibile" :option="tableOption" @getSelectedRows="getSelectedRows" :key="timer" @newKey="newKey"> </global-modal-template>
    <export-comp ref="exportComp" page-api-url="mes-product/stat-production-progress/page" :option="option" :page="page" :query="query" file-name="生产进度查询"> </export-comp>
  </basic-container>
</template>

<script>
import { getList, getDetail, getProductProgress, getProductInfo } from '@/api/product/production-schedule-query';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp/index';
import userTableConfig from '@/components/user-table-config/index.vue';
import GlobalModalTemplate from '@/components/common-modal-template/global-modal-template.vue';
export default {
  components: {
    // userTableConfig,
    GlobalModalTemplate,
    exportComp
  },
  mixins: [],
  data() {
    return {
      pageCode: 'sectionDefinition',
      visible: false,

      form: {},
      data: [],
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      timer: new Date().getTime(),
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuSpan: 4,
        labelWidth: 150,
        dialogCustomClass: 'custom-class',
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: false,
        viewBtnText: '详情',
        viewTitle: '详情',
        editBtn: false,
        delBtn: false,
        refreshBtn: true,
        excelBtn: false,
        selection: true,
        dialogDrag: false, // 开启拖拽功能
        dialogClickModal: false,
        columnBtn: false,
        showSummary: false,
        searchLabelWidth: 100,
        menuWidth: 100,
        column: [
          {
            label: '工单号',
            type: 'search',
            prop: 'workOrderNo',
            search: true,
            searchOrder: 1,
            placeholder: '请输入 工单号',
            appendClick: () => {
              this.workOrderNoCommonModal();
            }
          },
          {
            label: '工单状态',
            prop: 'woStatus',
            searchOrder: 6,
            type: 'select',
            search: true,
            width: 80,
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order'
          },
          {
            label: '销售订单',
            prop: 'salesOrderNo',
            search: true,
            searchOrder: 2
          },
          {
            label: '产品编码',
            prop: 'productPartNo',
            searchOrder: 3,
            search: true
          },
          {
            label: '产品型号',
            prop: 'productPartModel',
            searchOrder: 4,
            search: true
          },
          {
            label: '客户编码',
            type: 'search',
            prop: 'customerCode',
            search: true,
            searchOrder: 6,
            placeholder: '请输入 客户编码',
            appendClick: () => {
              this.customerCodeCommonModal();
            }
          },
          {
            label: '计划数量',
            prop: 'planQuantity'
          },
          {
            label: '投入数',
            prop: 'inputQuantity'
          },
          {
            label: '完工数',
            prop: 'completedQuantity'
          },
          {
            label: '生产进度',
            prop: 'productionProgress',
            formatter: (val, value, label) => {
              if (value) {
                return value + '%';
              }
            }
          },
          {
            label: '要求发货时间',
            prop: 'planStartTime',
            viewDisplay: false
          },
          {
            label: '计划开工时间',
            prop: 'planStartTime',
            viewDisplay: false
          },
          {
            label: '实际开工时间',
            prop: 'actualStartTime',
            viewDisplay: false
          },
          {
            label: '计划完工时间',
            prop: 'planEndTime',
            viewDisplay: false
          },
          {
            label: '实际完工时间',
            prop: 'actualEndTime',
            viewDisplay: false
          },

          {
            label: '按时完成',
            prop: 'finishedOnTime',
            type: 'select',
            viewDisplay: false,
            dicData: [
              {
                label: '是',
                value: true
              },
              {
                label: '否',
                value: false
              }
            ]
          },
          {
            label: '计划开工时间',
            prop: 'createTime',
            search: true,
            searchSpan: 12,
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            defaultTime: ['00:00:00', '23:59:59'],
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
            hide: true
          },
          {
            label: '工单下达时间',
            prop: 'woCreateTime',
            search: true,
            searchSpan: 12,
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            defaultTime: ['00:00:00', '23:59:59'],
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
            hide: true
          }
        ]
      },
      showDialog: false,
      basicInfoOption: {
        emptyBtn: false,
        submitBtn: false,
        span: 6,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo',
            disabled: true
          },
          {
            label: '产品编码',
            prop: 'productPartNo',
            disabled: true
          },
          {
            label: '产品名称',
            prop: 'productPartName',
            disabled: true
          },
          {
            label: '产品型号',
            prop: 'productPartModel',
            disabled: true
          },
          {
            label: '工单数量',
            prop: 'planQuantity',
            disabled: true
          },
          {
            label: '工单状态',
            prop: 'status',
            disabled: true,
            type: 'select',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order'
          },
          {
            label: '客户编码',
            prop: 'customerCode',
            disabled: true
          },
          {
            label: '客户名称',
            prop: 'customerName',
            disabled: true
          }
        ]
      },
      basicInfo: {},
      productProgressOption: {
        height: '300',
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        menu: false,
        refreshBtn: false,
        excelBtn: false,
        selection: false,
        columnBtn: false,
        column: [
          {
            label: '日期',
            prop: 'createTime'
          },
          {
            label: '产线',
            prop: 'lineName'
          },
          {
            label: '工段',
            prop: 'sectionName'
          },
          {
            label: '投入数量',
            prop: 'inputQuantity'
          },
          {
            label: '完工数量',
            prop: 'completedQuantity'
          },
          {
            label: '累计工时',
            prop: 'totalWorkHour'
          },
          {
            label: '不良数量',
            prop: 'ngQuantity'
          },
          {
            label: '在制数量',
            prop: 'wipQuantity'
          }
        ]
      },
      productProgressData: [],
      selectIndex: '',
      infoSelectionList: [], // 发货产品信息勾选数据
      infoOption: {
        selection: false,
        editBtn: false,
        height: 270,
        border: true,
        menuWidth: 100,
        addBtn: false,
        delBtn: true,
        menu: false,
        columnBtn: false,
        refreshBtn: false,
        tip: false,
        column: [
          {
            label: '工序',
            prop: 'routeName'
          },

          {
            label: '已过站数量',
            prop: 'completedQuantity'
          },
          {
            label: '待过站数量',
            prop: 'waitingQuantity'
          }
        ]
      },
      infoData: [],
      exportBtnLoading: false,
      visibile: false,
      modalName: '', //打开公用模态框名称
      tableOption: {} //公用模态框配置信息
    };
  },

  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.pallet_add, false),
        viewBtn: this.vaildData(this.permission.pallet_view, false),
        delBtn: this.vaildData(this.permission.pallet_delete, false),
        editBtn: this.vaildData(this.permission.pallet_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    closeDialog() {
      this.basicInfo = {};
      this.productProgressData = [];
      this.infoData = [];
      this.selectIndex = '';
    },
    beforeClose(done) {
      done();
    },
    beforeOpen(row, type) {},

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionLeftChange(list) {
      this.leftSelectionList = list;
    },
    selectionRightChange(list) {
      this.rightSelectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if (this.query.createTime) {
        this.query.queryStartPlanStartTime = this.$moment(this.query.createTime[0]).format('YYYY-MM-DD HH:mm:ss');
        this.query.queryEndPlanStartTime = this.$moment(this.query.createTime[1]).format('YYYY-MM-DD HH:mm:ss');
        delete this.query.createTime;
      }
      if (this.query.woCreateTime) {
        this.query.queryStartWoCreateTime = this.$moment(this.query.woCreateTime[0]).format('YYYY-MM-DD HH:mm:ss');
        this.query.queryEndWoCreateTime = this.$moment(this.query.woCreateTime[1]).format('YYYY-MM-DD HH:mm:ss');
        delete this.query.woCreateTime;
      }
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    /**
     * 重新加载公用表格弹窗组件
     */
    newKey() {
      this.timer = new Date().getTime();
    },
    goPath(workOrderNo) {
      this.$router.push({
        path: '/report/forward-trace-back-to',
        query: { workOrderNo: workOrderNo }
      });
    },
    // 获取选择的物料模态框行数据
    getSelectedRows(rowData) {
      switch (this.modalName) {
        case 'workOrderModal':
          this.search.workOrderNo = this._get(rowData, 'workOrderNo', '');
          this.$set(this.form, 'workOrderNo', this._get(rowData, 'workOrderNo', ''));
          break;
        case 'customerCode':
          this.search.customerCode = this._get(rowData, 'code', '');
          this.$set(this.form, 'customerCode', this._get(rowData, 'code', ''));
          break;
        default:
          break;
      }
    },
    workOrderNoCommonModal() {
      this.modalName = 'workOrderModal';
      this.visibile = true;
      this.tableOption = {
        modalTitle: '选择工单信息（双击进行选择）',
        url: '/api/mes-product/work-order/page',
        titleInfo: 'workOrderModal',
        isPage: true,
        rowSelectionType: 'single',
        menu: false,
        editBtn: false,
        menuWidth: 100,
        searchLabelWidth: 90,
        templateName: 'WORKORDER'
      };
    },
    customerCodeCommonModal() {
      this.modalName = 'customerCode';
      this.visibile = true;
      this.tableOption = {
        modalTitle: '选择客户编码（双击进行选择）',
        url: '/api/mes-admin/customer/page',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '客户名称',
            prop: 'name',
            search: true
          },
          {
            label: '客户编码',
            prop: 'code',
            search: true
          }
        ]
      };
    },
    viewDetail(row) {
      this.showDialog = true;
      getDetail(row.workOrderId).then((res) => {
        this.basicInfo = this._get(res, 'data.data', {});
        this.basicInfo.status = String(this.basicInfo.status);
      });
      getProductProgress(row.workOrderId).then((res) => {
        this.productProgressData = this._get(res, 'data.data', []);
      });
    },
    rowClick(row) {
      this.selectIndex = row.$index;
      getProductInfo({ workOrderId: this.basicInfo.id, lineId: row.lineId }).then((res) => {
        this.infoData = this._get(res, 'data.data', []);
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === this.selectIndex) {
        return 'select-row';
      }
      return '';
    }
  }
};
</script>

<style lang="scss" scoped>
.btn {
  padding: 9px 15px;
  font-size: 10px;
  height: 35px;
  background-color: #409eff !important;
  color: #cccccc !important;
}

.is-disabled {
  background-color: #fff !important;
  color: #cccccc !important;
}

.title {
  color: #000;
}

.title:hover {
  color: #000;
}

.el-transfer-button {
  display: flex;
  flex-direction: row;
  height: 100%;
  margin: 40px 12px 0;
  align-items: center;
}

.search-container {
  display: flex;
  justify-content: space-between;
}

.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.el-color-picker__trigger {
  height: 35px;
  width: 35px;
}
.custom-title {
  font-size: 16px;
  font-weight: bold;
  .mr10 {
    margin-right: 10px;
  }
}
.showDialog {
  ::v-deep .el-card__body {
    .avue-crud__menu {
      display: none;
    }
  }
  ::v-deep .el-table .select-row {
    background: #02a7f0;
  }
}
</style>
